function plot_tdma_signals(modulated_frames, num_users, slots_per_frame, bits_per_slot, guard_bits)
% plot_tdma_signals - 绘制TDMA信号时域波形
%
% 输入参数:
%   modulated_frames  - 调制后的TDMA帧信号
%   num_users        - 用户数量
%   slots_per_frame  - 每帧的时隙数
%   bits_per_slot    - 每个时隙的比特数
%   guard_bits       - 保护间隔比特数

% 参数计算
samples_per_bit = 8;  % 每比特的采样点数
slot_length = bits_per_slot * samples_per_bit;
guard_length = guard_bits * samples_per_bit;
total_slot_length = slot_length + guard_length;

% 只绘制前两帧(如果有)
frames_to_show = min(2, length(modulated_frames));

% 创建新图形
figure('Name', 'TDMA时域信号', 'Position', [100, 100, 1200, 600]);

% 不同用户使用不同颜色
colors = {'r', 'g', 'b', 'c', 'm', 'y', 'k', [0.5 0.5 0.5]};

% 绘制信号幅度和相位
for f = 1:frames_to_show
    % 获取当前帧信号
    frame_signal = modulated_frames{f};
    
    % 幅度子图
    subplot(frames_to_show, 2, 2*f-1);
    hold on;
    
    % 绘制幅度波形
    plot(abs(frame_signal), 'k', 'LineWidth', 1.2);
    
    % 标记每个时隙的边界
    for slot = 1:slots_per_frame
        slot_start = (slot-1) * total_slot_length + 1;
        slot_end = slot_start + slot_length - 1;
        guard_end = slot_end + guard_length;
        
        % 计算该时隙对应的用户
        user = mod(slot-1, num_users) + 1;
        user_color = colors{mod(user-1, length(colors))+1};
        
        % 用彩色条标记数据部分
        patch([slot_start slot_end slot_end slot_start], [0 0 1.5 1.5], user_color, 'FaceAlpha', 0.3);
        
        % 用灰色标记保护间隔
        if guard_length > 0
            patch([slot_end+1 guard_end guard_end slot_end+1], [0 0 1.5 1.5], [0.8 0.8 0.8], 'FaceAlpha', 0.5);
        end
        
        % 标记用户ID
        text((slot_start+slot_end)/2, 1.3, ['用户' num2str(user)], 'HorizontalAlignment', 'center');
    end
    
    title(['帧 #' num2str(f) ' - 信号幅度']);
    xlabel('样本');
    ylabel('幅度');
    ylim([0 1.5]);
    grid on;
    
    % 相位子图
    subplot(frames_to_show, 2, 2*f);
    hold on;
    
    % 绘制相位曲线
    phase = unwrap(angle(frame_signal));
    plot(phase, 'k', 'LineWidth', 1.2);
    
    % 标记每个时隙的边界(与幅度图相同)
    for slot = 1:slots_per_frame
        slot_start = (slot-1) * total_slot_length + 1;
        slot_end = slot_start + slot_length - 1;
        guard_end = slot_end + guard_length;
        
        user = mod(slot-1, num_users) + 1;
        user_color = colors{mod(user-1, length(colors))+1};
        
        % 计算相位范围以便绘制背景色
        phase_min = min(phase) - 1;
        phase_max = max(phase) + 1;
        
        % 用彩色条标记数据部分
        patch([slot_start slot_end slot_end slot_start], [phase_min phase_min phase_max phase_max], user_color, 'FaceAlpha', 0.3);
        
        % 用灰色标记保护间隔
        if guard_length > 0
            patch([slot_end+1 guard_end guard_end slot_end+1], [phase_min phase_min phase_max phase_max], [0.8 0.8 0.8], 'FaceAlpha', 0.5);
        end
    end
    
    title(['帧 #' num2str(f) ' - 信号相位']);
    xlabel('样本');
    ylabel('相位 (弧度)');
    grid on;
end

% 添加总标题
sgtitle('TDMA信号时域波形 - 时间划分多址接入');

end 